package com.easylive.web.controller;

import com.easylive.web.annotation.GlobalInterceptor;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.*;
import com.easylive.entity.po.UserAction;
import com.easylive.entity.po.UserFocus;
import com.easylive.entity.po.UserInfo;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.query.UserActionQuery;
import com.easylive.entity.query.UserFocusQuery;
import com.easylive.entity.query.VideoInfoQuery;
import com.easylive.entity.request.uhome.WebUHomeLoadVideoListReqVO;
import com.easylive.entity.request.uhome.WebUHomeUpdateUserInfoReqVO;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.UserInfoVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.UserActionService;
import com.easylive.service.UserFocusService;
import com.easylive.service.UserInfoService;
import com.easylive.service.VideoInfoService;
import com.easylive.utils.CopyTools;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yudaxian
 * @DateTime: 2024/12/2 16:26
 **/
@RestController
@RequestMapping("/uhome")
public class UHomeController extends ABaseController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private UserFocusService userFocusService;

    @Resource
    private UserActionService userActionService;

    @PostMapping("/getUserInfo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getUserInfo(@Validated @NotBlank String userId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        UserInfo userDetailInfo = userInfoService.getUserDetailInfo(tokenUserInfoDto.getUserId() == null ? null : tokenUserInfoDto.getUserId(), userId);

        UserInfoVO userInfoVO = CopyTools.copy(userDetailInfo, UserInfoVO.class);

        return getSuccessResponseVO(userInfoVO);
    }

    /**
     * 更新用户信息 不需要userId
     * @param reqVO
     * @return
     */
    @PostMapping("/updateUserInfo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO updateUserInfo(@Validated WebUHomeUpdateUserInfoReqVO reqVO) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        if (null == tokenUserInfoDto) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        UserInfo userInfo = CopyTools.copy(reqVO, UserInfo.class);
        userInfoService.updateUserInfo(userInfo, tokenUserInfoDto);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/saveTheme")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveTheme(@Validated @NotNull @Min(1) @Max(10) Integer theme) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        String userId = tokenUserInfoDto.getUserId();

        UserInfo updateInfo = new UserInfo();
        updateInfo.setTheme(theme);
        userInfoService.updateUserInfoByUserId(updateInfo, userId);

        return getSuccessResponseVO(null);
    }

    /**
     * 关注用户
     * @param focusUserId
     * @return
     */
    @PostMapping("/focus")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO focus(@Validated @NotBlank String focusUserId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        String userId = tokenUserInfoDto.getUserId();

        userFocusService.focusUser(userId, focusUserId);

        return getSuccessResponseVO(null);
    }

    /**
     * 查 focusUserId ，粉丝
     * 查 userId， 关注的人
     * @param focusUserId
     * @return
     */
    @PostMapping("/cancelFocus")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO cancelFocus(@Validated @NotBlank String focusUserId) {

        userFocusService.cancelFocusUser(getTokenUserInfoDto().getUserId(), focusUserId);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/loadFocusList")
    public ResponseVO loadFocusList(Integer pageNo, Integer pageSize) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        UserFocusQuery query = new UserFocusQuery();
        query.setPageNo(pageNo);
        query.setPageSize(pageSize);
        query.setOrderBy("u.focus_time desc");
        /**
         * 0: 关联关注
         * 1: 关联粉丝
         */
        query.setQueryType(QueryUserFocusEnum.QUERY_FOCUS.getCode());
        query.setUserId(tokenUserInfoDto.getUserId());

        PaginationResultVO<UserFocus> page = userFocusService.findListByPage(query);
        return getSuccessResponseVO(page);
    }

    @PostMapping("/loadFansList")
    public ResponseVO loadFansList(Integer pageNo, Integer pageSize) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserFocusQuery query = new UserFocusQuery();
        query.setPageNo(pageNo);
        query.setPageSize(pageSize);
        query.setOrderBy("u.focus_time desc");
        query.setQueryType(QueryUserFocusEnum.QUERY_FANS.getCode());
        query.setFocusUserId(tokenUserInfoDto.getUserId());

        PaginationResultVO<UserFocus> page = userFocusService.findListByPage(query);
        return getSuccessResponseVO(page);
    }

    @PostMapping("/loadVideoList")
    public ResponseVO loadVideoList(@Validated WebUHomeLoadVideoListReqVO reqVO) {

        VideoInfoQuery query = new VideoInfoQuery();
        /**
         * tyoe != null ：首页，默认展示前10个
         * type == null ：投稿，分页查询，按照orderType来排序
         */
        if (reqVO.getType() != null) {
            query.setPageSize(PageSize.SIZE10.getSize());
        }

        VideoOrderTypeEnum orderTypeEnum = VideoOrderTypeEnum.getByType(reqVO.getType());
        if (orderTypeEnum == null) {
            // 默认按照发布时间
            orderTypeEnum = VideoOrderTypeEnum.CREATE_TIME;
        }
        query.setOrderBy(orderTypeEnum.getField() + " desc");
        query.setVideoNameFuzzy(reqVO.getVideoName());
        query.setPageNo(reqVO.getPageNo());
        query.setUserId(reqVO.getUserId());

        PaginationResultVO<VideoInfo> page = videoInfoService.findListByPage(query);

        return getSuccessResponseVO(page);
    }

    @PostMapping("/loadUserCollection")
    public ResponseVO loadUserCollection(@Validated Integer pageNo, @NotBlank String userId) {

        UserActionQuery query = new UserActionQuery();

        query.setPageNo(pageNo);
        query.setOrderBy("action_time desc");
        query.setUserId(userId);
        query.setActionType(UserActionTypeEnum.VIDEO_COLLECT.getType());

        PaginationResultVO<UserAction> listByPage = userActionService.findListByPage(query);
        List<String> videoIds = listByPage.getList().stream().map(UserAction::getVideoId).collect(Collectors.toList());
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        if (videoIds.isEmpty()) {
            videoIds.add("");
        }
        videoInfoQuery.setVideoIds(videoIds);

        PaginationResultVO<VideoInfo> page = videoInfoService.findListByPage(videoInfoQuery);

        return getSuccessResponseVO(page);
    }


}
